<template>
  <div class="user-list-page f-c">
    <el-form inline>
      <el-form-item label="搜索成员">
        <el-input v-model="searchParams.value" clearable placeholder="" />
      </el-form-item>
      <el-form-item label="过滤账号">
        <el-input v-model="searchParams.value" clearable placeholder="" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-left: 15px" @click="onSearch">搜索</el-button>
        <el-button type="primary" style="margin-left: 15px" @click="onReset">清除</el-button>
      </el-form-item>
    </el-form>
    <div class="button-list2 f-r a-c">
      <el-button plain type="primary" @click="onAdd">添加成员</el-button>
      <el-button plain type="warning" @click="onImport">导入成员</el-button>
      <el-button plain type="primary">批量分田</el-button>
    </div>
    <div class="table-box">
      <el-table
        v-loading="loading"
        style="width: 100%"
        height="100%"
        stripe
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column type="index" align="center" label="序号" />
        <el-table-column prop="userName" label="姓名" align="center" />
        <el-table-column prop="phonenumber" label="账号/手机号" align="center" />
        <el-table-column prop="roleName" label="角色" align="center" />
        <el-table-column prop="deptName" label="部门" align="center" />
        <el-table-column prop="plantArea" label="种植面积(亩)" align="center" />
        <el-table-column prop="statusName" label="状态" align="center" />
        <el-table-column label="操作" fixed="right" width="300px">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="onEdit(scope.row)"
            > 编辑
            </el-button>
            <el-button
              v-if="updateStatusName[scope.row.status]"
              type="text"
              @click="updateStatus(scope.row)"
            >
              {{ updateStatusName[scope.row.status] }}账户
            </el-button>
            <el-button
              type="text"
              @click="onReadParent(scope.row)"
            >下级成员</el-button>
            <el-button
              type="text"
            >分田</el-button>
            <el-button
              type="text"
            >查看田块</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <User ref="userRef" />
    <UserParent ref="UserParentRef" />
    <Import ref="importRef" />
  </div>
</template>

<script>
import crud from '@/mixins/crud';
import { http_user } from '@/api';
import User from './user'
import UserParent from './user-parent'
import Import from '@/views/user/import';
const statusMap = {
  0: '正常',
  1: '停用'
}
export default {
  name: 'UserList',
  components: {
    Import,
    User,
    UserParent
  },
  mixins: [crud],
  props: {
    deptId: String
  },
  data() {
    return {
      searchParams: {
        search: ''
      },
      updateStatusName: {
        0: '冻结',
        1: '解冻'
      }
    }
  },
  watch: {
    deptId: {
      handler(newVal) {
        if (newVal) {
          this.searchParams = {
            search: ''
          }
        }
        this.initData()
      },
      immediate: true
    }
  },
  methods: {
    initData() {
      if (this.deptId) {
        this.loading = true
        http_user.list({
          ...this.searchParams,
          deptId: this.deptId
        }).then(res => {
          this.tableData = res.rows.map(el => {
            return {
              ...el,
              roleName: (el.roles || []).map(i => i.roleName).join(','),
              deptName: (el.depts || []).map(i => i.deptName).join(','),
              statusName: statusMap[el.status]
            }
          })
          this.loading = false
        })
      } else {
        this.tableData = []
      }
    },
    updateStatus({ userId, status }) {
      const _status = Number(status)
      if (isNaN(_status)) return
      http_user.updateStatus({
        userId,
        status: _status === 0 ? 1 : 0
      }).then(res => {
        this.$message.success(res.msg)
        this.initData()
      })
    },
    onAdd() {
      this.$refs.userRef.onAdd()
    },
    onEdit(item) {
      this.$refs.userRef.onEdit(item)
    },
    onImport() {
      this.$refs.importRef.dialogVisible = true
    },
    // 下级成员
    onReadParent(item) {
      this.$refs.UserParentRef.open(item)
    }
  }
}
</script>

<style scoped lang="scss">
.user-list-page {
  height: calc(100% - 100px);
}
</style>
